package com.expriment.polynomial;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class ResultOfPolynomial {
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;
    private static final String filePath = "D://学习资料//论文资料汇总//我的毕业论文//程序清单//测试FHOPE同态加乘的性能//同态加乘性能测试.xls";
    public void createSheet() {
        wb = new HSSFWorkbook();
        sheet = wb.createSheet("FHOPE算法同态加乘运算性能测试");
        row = sheet.createRow(0);
        cell = row.createCell(0);
        cell.setCellValue("执行次数");
        cell = row.createCell(1);
        cell.setCellValue("同态加法(ns)");
        cell = row.createCell(2);
        cell.setCellValue("同态乘法(ns)");
        try {
            FileOutputStream fout = new FileOutputStream(filePath);
            wb.write(fout);
            fout.close();
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void outputToExcel(int n, long additionTime, long multiplyTime) {
        try {
            FileInputStream fs = new FileInputStream(filePath); // 获取d://test.xls
            POIFSFileSystem ps = new POIFSFileSystem(fs); // 使用POI提供的方法得到excel的信息
            HSSFWorkbook wb = new HSSFWorkbook(ps);
            HSSFSheet sheet = wb.getSheet("FHOPE算法同态加乘运算性能测试"); 
            HSSFRow row = sheet.getRow(0); // 获取第一行（excel中的行默认从0开始，所以这就是为什么，一个excel必须有字段列头），即字段列头，便于赋值
            int lastRow = sheet.getLastRowNum();
            FileOutputStream out = new FileOutputStream(filePath); // 向d://test.xls中写数据
            row = sheet.createRow(lastRow + 1); // 在现有行号后追加数据
            row.createCell(0).setCellValue(String.valueOf(n)); // 设置第一个（从0开始）单元格的数据
            row.createCell(1).setCellValue(String.valueOf(additionTime));
            row.createCell(2).setCellValue(String.valueOf(multiplyTime));
            out.flush();
            wb.write(out);
            out.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
